package com.coins.common.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.fastjson.JSONObject;
import com.coins.agent.HTTPEvents;

public class Constants {

	public static String DEFAULT_REDISHEAD;
	public static String DEFAULT_REDISQUEUE = "release:hospital:monitor";
	public static String DEFAULT_WEBSOCKETTOPIC = "/topic/msg";

	public static final String format1 = "yyyy-MM-dd";
	public static final String format2 = "yyyy-MM-dd HH:mm:ss";
	public static final String format3 = "yyyy-MM-dd HH:mm";
	public static final String format4 = "HH:mm";

	/** 推送结果 */
	public static ConcurrentHashMap<String, List<JSONObject>> pushQueue = new ConcurrentHashMap<String, List<JSONObject>>();
	/** 缓存参数 */
	public static ConcurrentHashMap<HTTPEvents, List<String>> params = new ConcurrentHashMap<HTTPEvents, List<String>>();

	/**
	 * 功能：日期转字符
	 */
	public static String convert(Date date, String format) {
		String retValue = null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		retValue = sdf.format(date);
		return retValue;
	}

	/**
	 * 功能：日期转字符
	 */
	public static Date formatDate(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date $date = null;
		try {
			String retValue = sdf.format(date);
			$date = sdf.parse(retValue);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return $date;
	}

	/**
	 * 功能：日期转字符
	 */
	public static Date convert(String date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date $date = null;
		try {
			$date = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return $date;
	}

	/**
	 * 功能：从网络下载图片
	 */
	public static String downloadAvatar(String folder, String avatarUrl) throws MalformedURLException, IOException, FileNotFoundException {
		if (folder != null && folder.trim().length() > 0) {
			File f = new File(folder);
			if (!f.exists()) {
				f.mkdirs();
			}
			String tempDir = folder + File.separator + avatarUrl.substring(avatarUrl.lastIndexOf("/") + 1);
			URL urlss = new URL(avatarUrl);
			URLConnection con = urlss.openConnection();
			con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
			InputStream is = con.getInputStream();
			byte[] bs = new byte[1024];
			int len;
			OutputStream os = new FileOutputStream(tempDir);
			while ((len = is.read(bs)) != -1) {
				os.write(bs, 0, len);
			}
			os.close();
			is.close();
			return tempDir;
		}
		return null;
	}
}
